<template>
  <div class="departments-container">
    <div class="app-container">
      <h2>
        <el-card v-loading="loading" class="tree-card">
          <!-- 用了一个行列布局 -->
          <TreeTools :node-data="company" :is-show="true" @clickAddDepet="clickAddDepetFn" />
          <!-- 内容 -->
          <!-- tree的节点宽度默认由宽度撑开 可以设置宽度百分百-->
          <el-tree :data="departs" :props="defaultProps" default-expand-all>
            <template #default="{ data }">
              <TreeTools :node-data="data" @editDepet="handleEditDepet" @delDepartment="getDepartments" @clickAddDepet="clickAddDepetFn" />
            </template>
          </el-tree>
        </el-card>
        <AddDept
          ref="addDepet"
          :show-dialog.sync="showDialog"
          :node-data="nodeData"
          :departs-list="departsList"
          @AddDepet="getDepartments"
        />
      </h2>
    </div>
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import AddDept from './components/AddDept.vue'
import { reqGetDepartments } from '@/api/departments'
import { transListToTree } from '@/utils/index'
export default {
  name: 'Departments',
  components: {
    TreeTools,
    AddDept
  },
  data() {
    return {
      departs: [],
      departsList: [],
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      company: { name: '江苏传智播客教育科技有限公司', manager: '负责人' },
      showDialog: false,
      nodeData: {},
      loading: true
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      this.loding = true
      await new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, 500)
      })
      const { data: { depts, companyName }} = await reqGetDepartments()
      this.company.name = companyName
      this.company.id = ''
      this.departsList = depts
      const newDepts = transListToTree(depts, '')
      this.departs = newDepts
      this.loading = false
    },
    clickAddDepetFn(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
    },
    handleEditDepet(nodeData) {
      this.showDialog = true
      this.nodeData = nodeData
      this.$nextTick(() => {
        this.$refs.addDepet.EditDepartDetail()
      })
    }
  }
}
</script>

<style lang= "scss" scoped>
.tree-card {
  padding: 30px 30px;
  font-size:14px;
  font-weight: 400;
}
.el-tree {
  ::v-deep {
    /* // 小三角的样式, 去掉默认的小三角的旋转效果 */
    .el-tree-node__expand-icon.expanded {
      -webkit-transform: rotate(0deg);
      transform: rotate(0deg);
    }
    /* // 有子节点 且未展开 + */
    .el-icon-caret-right:before {
      background: url("~@/assets/common/add-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* 有子节点 且已展开 - */
    .el-tree-node__expand-icon.expanded.el-icon-caret-right:before{
      background: url("~@/assets/common/minus-circle.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
    /* // 没有子节点 */
    .el-tree-node__expand-icon.is-leaf::before  {
      background: url("~@/assets/common/user-filling.png") no-repeat 0 0;
      content: '';
      display: block;
      width: 16px;
      height: 16px;
      font-size: 16px;
      background-size: 16px;
    }
  }
}
</style>
